import { Injectable } from "@angular/core";

import { ServiceDefinition, DataTypeEnum } from './model/provider.definition';
import { RESTfulProvider } from './provider/restful.provider';
import { WebSocketProvider } from './provider/websocket.provider';
import { MockProvider } from './provider/mock.provider';
import { IProvider } from './provider.interface';
import { Http } from '@angular/http';
import { SelectionType } from '../../ui-framework/common/tree-component/module/tree.module';

@Injectable()
export class GeneralServiceProvider implements IProvider {
    definition: ServiceDefinition;
    pilot: any;
    providerInstance: any;
    constructor(definition: ServiceDefinition, pilot?: any) {
        this.definition = definition;
        this.pilot = pilot;
        switch (this.definition.dataType) {
            case DataTypeEnum.RESTful:
                this.providerInstance = new RESTfulProvider(definition.serviceConfig);
                break;
            case DataTypeEnum.webSockt:
                this.providerInstance = new WebSocketProvider(definition.webSocketUrl, this.pilot, definition.serviceConfig);
                break;
            case DataTypeEnum.mock:
                this.providerInstance = new MockProvider(definition.serviceConfig, definition.mockData);
                break;
            default:
                break;
        };
    }

    set(http: Http): void {
        this.providerInstance.set(http);
    }

    // start =====  general functions of RESTFul & Websocket & Mock =====
    /**
     * Common Interface Funtion - query
     * 
     * @param {Function} sucCallback
     * @param {Function} failedCallback
     * 
     * @memberOf IProvider
     */
    query(sucCallback: Function, failCallback: Function): void {
        this.providerInstance.query(sucCallback, failCallback);
    }

    /**
     * Common Interface Funtion - get
     * 
     * @param {*} paras
     * @param {Function} sucCallback
     * @param {Function} failCallback
     * 
     * @memberOf IProvider
     */
    get(paras: any, sucCallback: Function, failCallback: Function): void {
        this.providerInstance.get(paras, sucCallback, failCallback);
    }

    /**
     * Common Interface Funtion - add
     * 
     * @param {*} paras
     * @param {Function} sucCallback
     * @param {Function} failCallback
     * 
     * @memberOf IProvider
     */
    add(paras: any, sucCallback: Function, failCallback: Function): void {
        this.providerInstance.add(paras, sucCallback, failCallback);
    }

    /**
     * Common Interface Funtion - update
     * 
     * @param {*} paras
     * @param {Function} sucCallback
     * @param {Function} failCallback
     * 
     * @memberOf IProvider
     */
    update(paras: any, sucCallback: Function, failCallback: Function): void {
        this.providerInstance.update(paras, sucCallback, failCallback);
    }

    /**
     * Common Interface Funtion - delete
     * 
     * @param {*} paras
     * @param {Function} sucCallback
     * @param {Function} failCallback
     * 
     * @memberOf IProvider
     */
    delete(paras: any, sucCallback: Function, failCallback: Function): void {
        this.providerInstance.delete(paras, sucCallback, failCallback);
    }
    // end ===== general functions of RESTFul & Websocket =====  

    // start ===== special functions of Websocket =====
    /**
     * Websocket Function - invoke proxy
     * 
     * @param {*} paras
     * @param {Function} sucCallback
     * @param {Function} failCallback
     * 
     * @memberOf IProvider
     */
    invokeProxy(paras: any, sucCallback: Function, failCallback: Function): void {
        this.providerInstance.invokeProxy(paras, sucCallback, failCallback);
    }

    /**
     * Websocket Function - stop request via requestId
     * 
     * @param {number} requestId
     * @param {Function} sucCallback
     * @param {Function} failCallback
     * 
     * @memberOf IProvider
     */
    stopRequest(requestId: number, sucCallback: Function, failCallback: Function): void {
        this.providerInstance.stopRequest(requestId, sucCallback, failCallback);
    }

    /**
     * Websocket Function - stop all requests
     * 
     * @param {Function} sucCallback
     * @param {Function} failCallback
     * 
     * @memberOf IProvider
     */
    stopAllRequests(sucCallback: Function, failCallback: Function): void {
        this.providerInstance.stopAllRequests(sucCallback, failCallback);
    }

    /**
     * Websocket Function - close connection
     * 
     * @param {Function} sucCallback
     * @param {Function} failCallback
     * 
     * @memberOf IProvider
     */
    closeConnection(sucCallback: Function, failCallback: Function): void {
        this.providerInstance.closeConnection(sucCallback, failCallback);
    }

    /**
     * Websocket Function - open connection
     * 
     * @param {Function} sucCallback
     * @param {Function} failCallback
     * 
     * @memberOf IProvider
     */
    openConnection(token: string, sucCallback: Function, failCallback: Function): void {
        this.providerInstance.openConnection(sucCallback, failCallback);
    }
    // end ===== special functions of Websocket =====
}